

import os
import gtk
import gtk.glade
import gobject
from BuildWizException import BuildWizException
from Variable import Variable

class DlgImportVariables:
    def __init__( self, wizard, platfdestname, previousVars, newVars):
        self.__wiz = wizard
        self.__platfDestName = platfdestname
        self.__newVars = newVars
        self.__previousVars = previousVars
        self.__glade = self.__wiz.getPreferences().getGUIFile( "importvarswindow")
        if not os.path.isfile( self.__glade):
            raise BuildWizException( "DlgImportVariables: libglade could not open - [%s]" % self.__glade)
        self.__xml = gtk.glade.XML( self.__glade)
        if self.__xml:
            self.__win = self.__xml.get_widget( "DlgImportVariables")
            self.__win.connect( "destroy", self.__onCancel)
            self.__handlers = { "on_comboPlatf_changed": self.__onComboPlatfChanged
                              , "on_btnOrig2Dest_clicked": self.__sendVar2Dest 
                              , "on_btnDest2Orig_clicked": self.__deleteVarFromDest
                              , "on_btnOK_clicked": self.__onOK
                              , "on_btnCancel_clicked": self.__onCancel }
            self.__xml.signal_autoconnect( self.__handlers)
            self.__treeviewOrig = self.__xml.get_widget( "treeviewOrig")
            self.__storeOrig = self.__setVarView( self.__treeviewOrig)
            self.__treeviewDest = self.__xml.get_widget( "treeviewDest")
            self.__storeDest = self.__setVarView( self.__treeviewDest, True)
            self.__entryDestPlatf = self.__xml.get_widget( "entryDestPlatf")
            self.__entryDestPlatf.set_text( self.__platfDestName)
            self.__btnOK = self.__xml.get_widget( "btnOK")
            self.__btnOK.set_sensitive( False)
            self.__btnCancel = self.__xml.get_widget( "btnCancel")

            self.__comboPlatf = self.__xml.get_widget( "comboPlatf")
            self.__liststore = gtk.ListStore( str)
            self.__comboPlatf.set_model( self.__liststore)
    
            self.__insertPlatformItems()

    def __insertPlatformItems( self):
        for item in self.__wiz.getPlatforms().values():
            strItem = item.getID()
            if strItem != self.__platfDestName:
                self.__liststore.append( [strItem])

    def run( self):
        return self.__win.run()

    def __appendVar2View( self, var, store):
        store.append( None, (var.getName(), var.getType(), var.getValue()))

    def __showValues( self, values, store):
        platfVars = values
        for varItem in platfVars:
            self.__appendVar2View( varItem, store) 

    def __onComboPlatfChanged( self, combobox):
        self.__storeOrig.clear()
        model = combobox.get_model()
        index = combobox.get_active()
        if index >= 0:
            self.__showValues( self.__wiz.getPlatformConfigVariables( model[ index][ 0]), self.__storeOrig)
        else:
            print "*** ERROR: importvars: undefined index"
        
    def __deleteVarFromDest( self, widget):
        selection = self.__treeviewDest.get_selection()
        model, modelIt, = selection.get_selected()
        if modelIt:
            path = model.get_path( modelIt)
            if self.__newVars.has_key( model[ path][ 0]):
                del( self.__newVars[ model[ path][ 0]])
            model.remove( modelIt) 
        if len( self.__newVars) > 0:
            self.__btnOK.set_sensitive( True)
        else:
            self.__btnOK.set_sensitive( False)

    def __verifyIfVarExists( self, var):
        return self.__newVars.has_key( var.getName()) or self.__previousVars.has_key( var.getName())

    def __sendVar2Dest( self, widget):
        selection = self.__treeviewOrig.get_selection()
        model, modelIt, = selection.get_selected()
        if modelIt:
            path = model.get_path( modelIt)
            var = Variable( model[ path][ 0], model[ path][ 1], model[ path][ 2])
            if not self.__verifyIfVarExists( var):
                self.__appendVar2View( var, self.__storeDest)
                self.__newVars[ model[ path][ 0]] = var
                self.__btnOK.set_sensitive( True)
            else:
                dlg = gtk.MessageDialog( None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                        "The variable [" + model[ path][ 0] + "] has been inserted before!")
                dlg.run()
                dlg.destroy()

    def __onCancel( self, btn):
        self.__win.destroy()

    def __onOK( self, btn):
        self.__win.destroy()

    def __setVarView( self, treeview, isValueEditable = False):
        store = gtk.TreeStore( gobject.TYPE_STRING
                              , gobject.TYPE_STRING
                              , gobject.TYPE_STRING)
        if treeview:
            treeview.set_model( store) 

            rendererPlatform = gtk.CellRendererText()
            rendererPlatform.set_property( 'editable', False)
            columnVar = gtk.TreeViewColumn( "VARIABLE"
                                          , rendererPlatform
                                          , text=0)
            rendererType = gtk.CellRendererText()
            rendererType.set_property( 'editable', False)
            columnType = gtk.TreeViewColumn( "TYPE"
                                           , rendererType
                                           , text=1)
            rendererValue = gtk.CellRendererText()
            rendererValue.set_property( 'editable', isValueEditable)
            rendererValue.connect( 'edited', self.__onCellEdited, store, 2)
            columnValue = gtk.TreeViewColumn( "VALUE"
                                            , rendererValue
                                            , text=2)
            treeview.append_column( columnVar)
            treeview.append_column( columnType)
            treeview.append_column( columnValue)
            treeview.set_search_column( 0)
            columnVar.set_sort_column_id( 0)

        return store

    def __onCellEdited( self, cell, path, newText, model, column):
        model[ path][ column] = newText
        self.__newVars[ model[ path][ 0]].setValue( newText)

